'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));


module.exports.scoreList = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 10;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('score').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('score').alias('score').join({
            table: 'student',
            join: 'left',
            as: 'stu',
            on: ['stu_id', 'stu_id']
        }).join({
            table: 'project',
            join: 'left',
            as: 'pro',
            on: ['pro_id', 'pro_id']
        }).where(where).order('score.score_id DESC').limit(start,limit).select().then(function(score_list){

            let data = {
                array:score_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"scoreList.html",data);
        });
    });
};

module.exports.scoreAdd = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);

    db.table('project').select().then(function(project){
        db.table('student').select().then(function(student){
            db.table('score').select().then(function(score){
                let Data = {
                    "project":project,
                    "student":student,
                    "score":score
                };

                CommonHelper.renderFile(req,res,"scoreAdd.html",Data);
            });
        });        
    });
};

module.exports.scoreAddForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    //先获取表单的数据

    let data = {
        "score_number":req.body.score_number,
        "score_time":Date.now(),
        "score_name":req.body.score_name,
        "pro_id":req.body.pro_id,
        "stu_id":req.body.stu_id
    };

    db.table('score').add(data).then(function(insertId){
        if(insertId)
        {
            //重定向
            res.redirect(`/score/scoreList`);
            return false;
        }else{
            //重定向
            res.redirect(`/score/scoreAdd`);
            return false;
        }
    });
};

module.exports.scoreEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let score_id = req.query.score_id;

    if(!score_id)
    {
        //重定向
        res.redirect(`/score/scoreList`);
        return false;
    }

    db.table('score').where(`score_id = ${score_id}`).find().then(function(score){
        db.table('project').select().then(function(project){
            db.table('student').select().then(function(student){
                let Data = {
                    "score":score,
                    "student":student,
                    "project":project
                };

                // console.log('----------------------------');
                // console.log(Data.project);

                CommonHelper.renderFile(req,res,"scoreEdit.html",Data);
            });
        });
    });
};

module.exports.scoreEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let score_id = req.query.score_id;

    if(!score_id)
    {
        //重定向
        res.redirect(`/score/scoreList`);
        return false;
    }

    db.table('score').where(`score_id = ${score_id}`).find().then(function(score){

        let data = {
            "score_number":req.body.score_number,
            "score_time":req.body.score_time,
            "score_name":req.body.score_name,
            "pro_id":req.body.pro_id,
            "stu_id":req.body.stu_id
        };

        db.table('score').where(`score_id = ${score_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/score/scoreList`);
                return false;
            }else{
                //重定向
                res.redirect(`/score/scoreEdit?score_id=${score_id}`);
                return false;
            }
        });
    });
};

module.exports.scoreDelete = function(req,res){

    let score_id = req.query.score_id;

    if(!score_id)
    {
        //重定向
        res.redirect(`/score/scoreList`);
        return false;
    }
    
    db.table('score').where(`score_id = ${score_id}`).delete().then(function(affectId){
        if(affectId){
            //重定向
            res.redirect(`/score/scoreList`);
            return false;
        }else{
            //重定向
            res.redirect(`/score/scoreList`);
            return false;
        }
    });
};